using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

using AmazonBookLinker.Properties;

namespace AmazonBookLinker
{
  internal partial class BookInfoForm : Form
  {
    private readonly Book _book;


    internal BookInfoForm( Book book )
    {
      _book = book;
      InitializeComponent();
      DisplayBookInformation();
    }


    private void DisplayBookInformation()
    {
      WriteBookInformation();
      FormatBookTitleInBold();
      UpdateBookCover();
    }


    private void UpdateBookCover()
    {
      picBookCover.Image = _book.Image;
    }


    private void WriteBookInformation()
    {
      string text = string.Format( "{0} by {1}", _book.Title, _book.Author );

      if ( !string.IsNullOrEmpty( _book.Price ) )
        text += string.Format( " (Buy it now for {0} at {1})", _book.Price, GetDomainName( _book.DetailsUrl ) );

      txtBookDetails.Text = text;
    }


    private void FormatBookTitleInBold()
    {
      txtBookDetails.Select( 0, _book.Title.Length );
      txtBookDetails.SelectionFont = new Font( FontFamily.GenericSansSerif, 10, FontStyle.Bold );
    }


    private static string GetDomainName( string uriString )
    {
      Uri uri = new Uri( uriString );
      return uri.Host.Substring( 4 );
    }


    private void btnClose_Click( object sender, EventArgs e )
    {
      DialogResult = DialogResult.OK;
      Close();
    }


    private void btnShopNow_Click( object sender, EventArgs e )
    {
      Process.Start( _book.DetailsUrl.Replace( Settings.Default.AssociateTag, Settings.Default.DefaultAssociateTag ) );
    }
  }
}